<template>
  <div class="app-container">
    <div class="u-flex u-flex-wrap">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="轮播图" prop="image1">
          <Upload v-model="form.image1" />
        </el-form-item>
        <el-form-item label="屏保" prop="image2">
          <!-- <UploadVideo v-model="form.video" /> -->
          <!-- <Upload v-model="form.image" /> -->
          <Upload v-model="form.image2" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { currentAds, createAds } from "@/api/user";
import Upload from "@/components/Upload/SingleImage4";
// import UploadVideo from "@/components/Upload/SingleImage5";
export default {
  // components: { Upload, UploadVideo},
  components: { Upload },
  data() {
    return {
      list: [],
      loading: false,
      // currentPage: 1,
      queryData: {
        status: "", //	商品状态 0 下架 1 上架
        name: "",
      },
      pagination: {
        pageSize: 1,
        total: 1,
      },
      typeStatus: [
        { id: "", name: "不限" },
        { id: 0, name: "商品详情" },
        { id: 1, name: "H5" },
      ],
      form: {
        image: [],
        image1: [],
        image2: [],
        video: []
      },
      rules: {
        image1: [{ required: true, message: "请上传轮播图", trigger: "change" }],
        image2: [{ required: true, message: "请上传屏保", trigger: "change" }],
        // video: [{ required: true, message: "请上传视频", trigger: "change" }]
      },
      goodsId: "",
    };
  },
  created() {
    this.currentAds();
  },
  computed: {
    currentPage: {
      get() {
        return this.queryData.pageIndex + 1;
      },
      set() {},
    },
  },
  methods: {
    currentAds() {
      currentAds().then((res) => {
        this.form.image1 = res.data.topBanners || [];
        this.form.image2 = res.data.screenBanners || [];
      });
    },
    hanleSearch() {
      this.queryData.pageIndex = 0;
      this.getList();
    },
    handleSizeChange(val) {
      // this.queryData.pageIndex = val - 1
      // this.getList()
    },
    handleCurrentChange(val) {
      this.queryData.pageIndex = val - 1;
      this.getList();
    },
    handleEdit(id) {
      this.$router.push({
        path: "/swiper/edit",
        query: {
          id: id,
        },
      });
    },
    handleDelete(id) {
      this.$confirm("确认要删除该轮播图么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.confirmDelete(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    confirmDelete(id) {
      bannerDelete({ id }).then((res) => {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.getList();
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid, b, c) => {
        if (valid) {
          createAds({screenBanners: this.form.image2, topBanners: this.form.image1}).then(() => {
            this.$alert("设置完成", null, {
              confirmButtonText: "确定",
              showCancelButton: false,
              type: "success",
              showClose: false
            })
              .then(() => {
                this.currentAds()
              })
              .catch((err) => {
                this.currentAds()
              });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
</style>
